import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreWeappService extends BaseService<any> {
  private readonly logger = new Logger(CoreWeappService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * app
   * 对应 PHP: CoreWeappService_core::app()
   * 逻辑类型: undefined - undefined
   */
  async app(site_id: number) {
    // 基于PHP真实逻辑: app
    // PHP原文: $core_weapp_service = new CoreWeappConfigService();         $weapp_config = $core_weapp_service->getWeappConfig($site_id);          if ($weapp_config[...
const core_weapp_service = this.coreWeappConfigService;
        const weapp_config = core_weapp_service.getWeappConfig(site_id);

        if (weapp_config[ 'is_authorization' )) {
            const authorization_info = core_weapp_service.getWeappAuthorizationInfo(site_id)[ 'authorization_info' ];
            return CoreOplatformService.app().getMiniAppWithRefreshToken(weapp_config[ 'app_id' ), authorization_info[ 'authorizer_refresh_token' )];
  }

  /**
   * appApiClient
   * 对应 PHP: CoreWeappService_core::appApiClient()
   * 逻辑类型: undefined - undefined
   */
  async appApiClient(site_id: number) {
    // 基于PHP真实逻辑: appApiClient
    // PHP原文: return self::app($site_id)->getClient();     }      /**      * 刷新token      * @param int $site_id      * @return void      * @throws InvalidArgumentEx...
return self.app(site_id).getClient(];
    }

    /**
     * 刷新token
     * @param int site_id
     * @return void
     * @throws InvalidArgumentBusinessException
     */
    public static function refreshToken(int site_id)
    {
        const core_weapp_service = this.coreWeappConfigService;
        const weapp_config = core_weapp_service.getWeappConfig(site_id);

        if (!weapp_config[ 'is_authorization' )) {
            self.app(site_id).getAccessToken().refresh(];
  }

  /**
   * refreshToken
   * 对应 PHP: CoreWeappService_core::refreshToken()
   * 逻辑类型: undefined - undefined
   */
  async refreshToken(site_id: number) {
    // 基于PHP真实逻辑: refreshToken
    // PHP原文: $core_weapp_service = new CoreWeappConfigService();         $weapp_config = $core_weapp_service->getWeappConfig($site_id);          if (!$weapp_config...
const core_weapp_service = this.coreWeappConfigService;
        const weapp_config = core_weapp_service.getWeappConfig(site_id);

        if (!weapp_config[ 'is_authorization' )) {
            self.app(site_id).getAccessToken().refresh(];
  }

  /**
   * qrcode
   * 对应 PHP: CoreWeappService_core::qrcode()
   * 逻辑类型: undefined - undefined
   */
  async qrcode(site_id: number, page: number, data: number, filepath: number, width: number) {
    // 基于PHP真实逻辑: qrcode
    // PHP原文: $scene = [];         foreach ($data as $v) {             $scene[] = $v[ 'key' ] . '-' . $v[ 'value' ];...
const scene = [];
        foreach (data as v) {
            scene[] = v[ 'key' ]  + '-' . v[ 'value' ];
  }

  /**
   * getWeappPreviewImage
   * 对应 PHP: CoreWeappService_core::getWeappPreviewImage()
   * 逻辑类型: undefined - undefined
   */
  async getWeappPreviewImage(site_id: any) {
    // 基于PHP真实逻辑: getWeappPreviewImage
    // PHP原文: $app = self::appApiClient($site_id);         $response = $app->get('/wxa/get_qrcode');         if ($response->isFailed()) {             // 出错了，处理异常   ...
const app = self.appApiClient(site_id];
        const response = app.get('/wxa/get_qrcode');
        if (response.isFailed()) {
            // 出错了，处理异常
            throw new BusinessException('WECHAT_MINI_PROGRAM_CODE_GENERATION_FAILED');
  }
}
